
// 引入AES.js
var baseJs = require("../../common/base.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    state: "",
    clockList: [],
    // 删除状态,为每一个模拟的数据添加show 默认隐藏删除按钮
    show: false,
    //触摸坐标初始化
    touchStartX: 0,
    // 
    uid: "",
    userType: "",
    // 操作id
    // actionId: "",
    // maskFlag: true,
    // 
    index: '',
    SqId:"",//授权id
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    // console.log(options.uid);
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
    })
    // 如果是从授权的页面点进来
    if(options.uid){
      this.setData({
        SqId:options.uid
      })
    }
    // this.GetCustomGoodsType();
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    this.GetCustomGoodsType();
  },
  touchStart(e) {
    //设置初始位置的值为滑动的坐标
    this.setData({
      touchStartX: e.changedTouches[0].clientX,
    })
  },
  touchEnd(e) {
    // console.log(e)
    //获取当前点击index
    var index = e.currentTarget.dataset.index;
    //重新赋值
    var clockList = this.data.clockList;
    var show = this.data.show;
    //判断初始值是否大于移动之后的值+50 如果大于判断是左划删除 删除按钮显示
    if (this.data.touchStartX > e.changedTouches[0].clientX + 50) {
      clockList[index].show = true;
    }
    //判断初始值是否小于等于初始值 如果小于判断是用户右滑 删除按钮隐藏
    if (this.data.touchStartX <= e.changedTouches[0].clientX) {
      clockList[index].show = false;
    }
    //更新clockList
    this.setData({
      clockList,
      show: true,
    })
  },
  //删除
  delClock: function (e) {
    var that = this;
    var clockList = this.data.clockList;
    var index = e.currentTarget.dataset.id;
    console.log(index);
    // this.data.clockList.splice(index, 1);
    wx.showModal({
      title: '提示',
      content: '确定删除该分组？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BusinessDeleteCustomGoodsType&TypeId=" + index),
            success: function (res) {
              clockList.splice(index, 1);
              var json = JSON.parse(baseJs.decryUrl(res.data));
              console.log(json)
              if (json.ResultFlag == 1) {
                // wx.setStorageSync('index', json.ResultData.Id);
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  clockList: clockList,
                })
                that.GetCustomGoodsType();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else if (res.cancel) {

        }
        // //删除之后,重新赋值
        // this.setData({
        //   clockList: this.data.clockList
        // })
      }
    })
  },
  // 修改
  reviseClock: function (e) {
    var clockList = this.data.clockList;
    var index = e.currentTarget.dataset.id;
    console.log(index);
    wx.navigateTo({
      url: '/packageA/reviseName/reviseName?TypeId=' + index,
    })
  },
  //点击分组跳转到我的产品页面
  ListLook: function (e) {
    // console.log(e);
    var clockList = this.data.clockList;
    var index = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    // console.log(index);
    wx.navigateTo({
      url: '/packageA/uploadGoodsList/uploadGoodsList?TypeId=' + index + "&Sqid="+this.data.SqId + "&name="+name,//我的产品
    })
  },
  // 获取分组产品列表
  GetCustomGoodsType: function () {
    var that = this;
    // var uid = this.data.uid;
    if(that.data.SqId){
      var uid = this.data.SqId;
    }else{
      var uid = this.data.uid;
    }
    var pageIndex = this.data.pageIndex;
    var clockList = this.data.clockList;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BusinessGetCustomGoodsType&UID=" + uid + "&Type=" + 0),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(json)
        clockList = [];
        for (var i = 0; i < json.length; i++) {
          clockList.push(json[i])
        }
        that.setData({
          clockList: clockList,
        })
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })
  },
})